package com.grmasa.soundtoggle;

import android.app.PendingIntent;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.drawable.Icon;
import android.os.Build;
import android.provider.Settings;
import android.service.quicksettings.Tile;
import android.service.quicksettings.TileService;
import android.util.Log;
import android.widget.Toast;
import androidx.annotation.RequiresApi;

public class BatterySettingsTileService extends TileService {
    private static final String TAG = "BatteryTile";
    private static final int REQUEST_CODE_HONOR = 1001;
    private static final int REQUEST_CODE_DEFAULT = 1002;

    @Override
    public void onClick() {
        super.onClick();
//        openManufacturerSpecificSettings();
        openBatterySettings();
    }

    private void openBatterySettings() {


        // 1. 构造要跳转的普通 Intent
//        Intent intent = new Intent(Settings.ACTION_BATTERY_SAVER_SETTINGS);
                Intent intent = new Intent(Intent.ACTION_POWER_USAGE_SUMMARY);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        // 2. 把 Intent 包成 PendingIntent
        PendingIntent pendingIntent = PendingIntent.getActivity(
                this,
                0,
                intent,
                PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
        );

        // 3. 使用新的 API 启动 Activity 并收起 QS 面板
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
            startActivityAndCollapse(pendingIntent);
        }
    }





    @Override
    public void onStartListening() {
        super.onStartListening();
        updateTileState();
    }

    private void updateTileState() {
        Tile tile = getQsTile();
        if (tile != null) {
            tile.setLabel("电池设置");
            tile.setIcon(Icon.createWithResource(this, R.drawable.ic_performance));
            tile.setState(Tile.STATE_ACTIVE);
            tile.updateTile();
        }
    }
}